<template>
    <el-dialog title="委托他人代办" :visible.sync="visible" width="600px" destroy-on-close>
        <el-form v-loading="loading" :rules="rules" ref="formData" :model="formData" status-icon>
            <el-form-item label="待办人用户名" prop="assigneeUserKey" label-width="120px">
                <el-select v-model="formData.assigneeUserKey" placeholder="请选择转办人">
                    <el-option
                            v-for="item in list"
                            :key="item.username"
                            :label="item.realName+'('+item.username+')'"
                            :value="item.username">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item align="center">
                <el-button type="primary" @click="submitForm('formData')" size="mini">提交</el-button>
                <el-button size="mini" @click="visible=false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>
<script>

export default {
    props: {
        taskId: String,
    },
    created() {
        this.getData();
    },
    data() {
        return {
            list: [],
            visible: false, // 弹出窗口，true弹出
            loading: false,
            formData: { // 提交表单数据
                taskId: null,
                assigneeUserKey: null
            },
            rules: {
                assigneeUserKey: [
                    {required: true, message: '请输入代办人用户名', trigger: 'blur'},
                ]
            }
        }
    },

    methods: {
        getData(){
            let vm = this;
            this.axios({
                url: '/myapi/user/turn/list',
                method: 'get'
            }).then(function (response) {
                if(response.data.code===200){
                    console.log(response.data);
                    vm.list = response.data.data.data;
                }
            });
        },
        // 提交表单数据
        submitForm(formName) {
            let vm = this;
            this.$refs[formName].validate( async (valid) => {
                if (valid) {
                    // 校验通过，提交表单数据
                    this.loading = true
                    try {
                      vm.formData.taskId = vm.taskId
                        vm.axios({
                            url: '/myapi/task/turn',
                            method: 'post',
                            params: {
                                taskId: vm.formData.taskId,
                                assigneeUserKey: vm.formData.assigneeUserKey
                            }
                        }).then(function (response) {
                            if(response.data.code===200){
                                // 刷新数据
                                vm.$parent.fetchData()
                                vm.$message.success('转办成功')
                                // 将表单清空
                                vm.$refs['formData'].resetFields()
                                // 关闭窗口
                                vm.visible = false
                            }
                        });
                      this.loading = false
                    } catch(e) {
                      this.loading = false
                    }
                }
            })
        },

    }
}
</script>
